//
//  DIPubQuality.m
//  NursingManages
//
//  Created by Zhen Huang on 17/6/6.
//  Copyright © 2017年 huangzhen. All rights reserved.
//

#import "DIPubQuality.h"
#import "Networking.h"
#import "InitDbTable.h"
#import <TMCache.h>
#import "HCommon.h"
@implementation DIPubQuality


-(instancetype)init{
    
    NSString *url = AppServiceConfig.GetServiceUrl;
    
    self.downloadUrl  = [NSString stringWithFormat:@"%@/%@?token=%@",url,@"getqcdata",[[TMCache sharedCache] objectForKey:@"deviceToken"]];
    
    
    self.downloadParam  = [NSString stringWithFormat:@"%@/%@?token=%@",url,@"GetParam",[[TMCache sharedCache] objectForKey:@"deviceToken"]];

    return self;
}


- (void) startDownloadParam:(void (^)())callback
{
    [Networking receiveDataWithURLString:self.downloadParam method:@"GET" body:nil Block:^(NSData *tempData) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:tempData options:NSJSONReadingMutableContainers error:nil];
        
        InitDbTable* initdb = [[InitDbTable alloc]init];
        Boolean flag =  [[dic valueForKey:@"flag"] boolValue];
        NSString *msg = (NSString*)[dic objectForKey: @"msg"];
        
        if(! flag ){
            [HCommon popupErrorMessage:msg];
            return;
        }
        
        NSArray *data = [dic objectForKey: @"data"];
        NSMutableArray *dataArray = [[NSMutableArray alloc]init];

        for (NSDictionary *paramDic in data) {
            
            Model_Hr_Param *model = [[Model_Hr_Param alloc]initWithDictionary:paramDic];
            
            [dataArray addObject:model];
            
        }
        
        [initdb deleteHrParam];
        
        
        NSLog(@"正在写入质量检查数据");
        [initdb insertParam:dataArray];
        
        if(callback){
            callback();
        }


    }];
}
-(void) startDownload:(void(^)())callback{
    NSLog(@"开始 质量检查");
    NSLog(@"开始 下载质量检查");


    [Networking receiveDataWithURLString:self.downloadUrl method:@"GET" body:nil Block:^(NSData *tempData) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:tempData options:NSJSONReadingMutableContainers error:nil];
        
        
        InitDbTable* initdb = [[InitDbTable alloc]init];

        Boolean flag =  [[dic valueForKey:@"flag"] boolValue];
        NSString *msg = (NSString*)[dic objectForKey: @"msg"];
        
        if(! flag ){
            [HCommon popupErrorMessage:msg];
            return;
        }
        NSDictionary *data = [dic objectForKey: @"data"];
        
        NSDictionary *typeArray = [data objectForKey: @"qc_type"];
        NSDictionary *qc_standardArray = [data objectForKey: @"qc_standard"];
        NSDictionary *qc_codeArray = [data objectForKey: @"qc_code"];
        NSDictionary *qc_deptArray = [data objectForKey:@"qc_dept"];
        NSDictionary *qc_dictArray = [data objectForKey:@"qc_dict"];
        NSDictionary *qc_specArray = [data objectForKey:@"qc_spec"];
        NSDictionary *qc_qc_spec_itemArray = [data objectForKey:@"qc_spec_item"];
        NSDictionary *qc_deducting_reasonsArray = [data objectForKey:@"qc_deducting_reasons"];
//        [HCommon dismissAnimated:NO];
//        [HCommon popupSuccessMessage:@"下载成功"];
        
        
        NSLog(@"下载成功");
        NSLog(@"共%lu条检查类型数据",(unsigned long)[typeArray count]);
        NSLog(@"共%lu条检查标准数据",(unsigned long)[qc_standardArray count]);
        NSLog(@"共%lu条检查指标数据",(unsigned long)[qc_codeArray count]);
        NSLog(@"共%lu条标准适用科室数据",(unsigned long)[qc_deptArray count]);
        NSLog(@"共%lu条夜查房查房时段数据",(unsigned long)[qc_dictArray count]);
        
        NSMutableArray *arrType     = [[NSMutableArray alloc]init];
        NSMutableArray *arrStandard = [[NSMutableArray alloc]init];
        NSMutableArray *arrCode     = [[NSMutableArray alloc]init];
        NSMutableArray *arrDept     = [[NSMutableArray alloc]init];
        NSMutableArray *arrDict     = [[NSMutableArray alloc]init];
        NSMutableArray *arrspec     = [[NSMutableArray alloc]init];
        NSMutableArray *arrspec_item = [[NSMutableArray alloc]init];
        NSMutableArray *arreasons  = [[NSMutableArray alloc]init];
        
        for (NSDictionary *diction in typeArray) {
            HQcTypeModel *model =[[HQcTypeModel alloc]initWithDictionary:diction];
            [arrType addObject:model];
        }
        
        for (NSDictionary *diction in qc_standardArray) {
            HQc_standardModel *model =[[HQc_standardModel alloc]initWithDictionary:diction];
            [arrStandard addObject:model];
        }
        
        for (NSDictionary *diction in qc_codeArray) {
            HQc_codeModel *model =[[HQc_codeModel alloc]initWithDictionary:diction];
            [arrCode addObject:model];
        }
        
        for (NSDictionary *diction in qc_deptArray) {
            HQc_deptModel *model =[[HQc_deptModel alloc]initWithDictionary:diction];
            [arrDept addObject:model];
        }
        
        for (NSDictionary *diction in qc_dictArray) {
            Model_Pub_Dictionary_Item *model =[[Model_Pub_Dictionary_Item alloc]initWithDictionary:diction];
            [arrDict addObject:model];
        }
        
        for (NSDictionary *diction in qc_specArray) {
            
            Model_Hr_Qc_Spec *model = [[Model_Hr_Qc_Spec alloc]initWithDictionary:diction];
            [arrspec addObject:model];
        }
        for (NSDictionary *diction in qc_qc_spec_itemArray) {
            
            Model_Hr_Qc_Spec_Item *model = [[Model_Hr_Qc_Spec_Item alloc]initWithDictionary:diction];
            
            [arrspec_item addObject:model];
        }
        
        
        for (NSDictionary *reasonDic in qc_deducting_reasonsArray) {
            
            Model_Hr_CodeReasonModel *reasonModel = [[Model_Hr_CodeReasonModel alloc]initWithDictionary:reasonDic];
            [arreasons addObject:reasonModel];
        }
        
        NSLog(@"正在清除原有质量检查数据");
        [initdb deleteHrQuality];
        
        [initdb deletePubDictionaryItem:@"019023"];
        
        NSLog(@"正在写入质量检查数据");
        [initdb insertHrQuality:arrCode arrQcType:arrType arrQcStandard:arrStandard arrQcDept:arrDept arrQcDict:arrDict arrspec:arrspec arrspec_item:arrspec_item arreasons:arreasons];
        
        if(callback){
            callback();
        }
        
        NSLog(@"完成 下载质量检查");
        
    }];
    
}

@end
